package com.example.administrator.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private final static int[] IMGID = new int[]{R.drawable.b1,R.drawable.b2,R.drawable.b3};
    private Gallery mGallery ;
    private int mImageId;
    private int mLevel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGallery = (Gallery) findViewById(R.id.gallery);

        mGallery.setAdapter(new GalleryAdapter(IMGID,this));
        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                //得到选择图片
                mImageId = IMGID[position];
                builder.setIcon(R.mipmap.ic_launcher).setTitle("请选择难度");
                builder.setPositiveButton("简单", onClickListener);
                builder.setNeutralButton("一般", onClickListener);
                builder.setNegativeButton("困难", onClickListener);
                builder.show();

            }
        });
    }
    private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    mLevel = 3;
//                    Toast.makeText(MainActivity.this,"简单"+mImageId,Toast.LENGTH_SHORT).show();
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    mLevel = 4;
//                    Toast.makeText(MainActivity.this,"一般"+mImageId,Toast.LENGTH_SHORT).show();
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    mLevel = 5;
//                    Toast.makeText(MainActivity.this,"困难"+mImageId,Toast.LENGTH_SHORT).show();
                    break;
            }
            Bundle bundle = new Bundle();
            bundle.putInt("level",mLevel);
            bundle.putInt("imageId", mImageId);
            Intent intent = new Intent(MainActivity.this,SecendActivity.class);
            intent.putExtras(bundle);

            startActivity(intent);
        }

    };
}
